<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\api\home;

use think\Controller;
use think\Db;
use think\Request;
use app\common\model\Currency as currencyModel;

class Publics extends Controller
{
    protected $requestData;

    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers:content-type');
        header('Access-Control-Request-Method:GET,POST');
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
            exit;
        }
        $request = Request::instance();
        $this->requestData = $request->param();
    }

    /**
     * 图片上传
     * @author
     */
    public function upload()
    {
        header('Access-Control-Allow-Origin:*');
        $request = Request()->file();
        $uploadImg = array();
        $_file_path = ROOT_DIR . 'upload/posts/';
        foreach ($request as $file) {
            $infoArr = array();
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . $_file_path);
            if ($info) {
                $infoArr['img'] = $_file_path . str_replace('\\', '/', $info->getSaveName());
                array_push($uploadImg, $infoArr); //$info->getFilename();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        echo json_encode($uploadImg);
        exit;
    }

    /**
     * 获取价格
     * @author
     * @throws \Exception
     */
    public function getPrice()
    {
        //$url = 'https://api.coinx.pro/rest/v1/ticker';
        //$params['symbol'] = 'VNS/USDT';
        $currencyInfo = Db::name('currency')->where('id', 2)->field('last_price')->find();
        if ($currencyInfo) {
            $data = [];
            $data['last'] = $currencyInfo['last_price'];
            $result = array(
                'status' => 1,
                'data' => $data,
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 1,
                'data' => 'get error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit;
    }


    /**
     * 获取所有币种
     * @author
     */
    public function getCurrencyList()
    {
        $postData = $this->requestData;
        $page = isset($postData['pageNo']) ? $postData['pageNo'] : 1;
        $page = $page - 1;
        $limit = isset($postData['pageSize']) ? $postData['pageSize'] : 10;
        //$currencyName = isset($postData['currencyName']) ? $postData['currencyName'] : '';
        $search = isset($postData['search']) ? $postData['search'] : '';
        //$contractAddress = isset($postData['contractAddress']) ? $postData['contractAddress'] : '';
        $currencyModel = new currencyModel();
        $currencyList = $currencyModel->currencyList($page,$limit,$search);
        $result = array(
            'status' => 1,
            'data' => $currencyList,
            'code' => 0,
        );
        echo json_encode($result);
        exit;
    }

    /**
     * 获取币种信息
     * @author
     */
    public function getCurrencyInfo()
    {
        $postData = $this->requestData;
        if (isset($postData['currencyId'])) {
            $currencyId = $postData['currencyId'];
        } else {
            $currencyId = '';
        }
        if (isset($postData['contractAddress'])) {
            $contractAddress = $postData['contractAddress'];
        } else {
            $contractAddress = '';
        }
        if ($currencyId || $contractAddress) {
            $currencyModel = new currencyModel();
            $currencyInfo = $currencyModel->currencyInfo($currencyId, $contractAddress);
            $result = array(
                'status' => 1,
                'data' => $currencyInfo,
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 0,
                'data' => 'params error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 更新价格
     * @author
     * @return bool
     * @throws \Exception
     * @throws \think\Exception
     */
    public function updatePrice(){
        $vnsUrl = 'https://api.coinx.pro/rest/v1/ticker';
        $params['symbol'] = 'VNS/USDT';
        $vnsData = http($vnsUrl, $params);
        if ($vnsData) {
            $vnsData = json_decode($vnsData, true);
            Db::name('currency')->where('id', 2)->update(array('last_price'=>$vnsData['last']));
        }
        $url = 'http://47.92.126.183:82/client/bancor/price/list';
        $data = http($url,[]);
        if($data){
            $priceData = json_decode($data,true);
            if($priceData['error_code'] == 0){
                $priceList = $priceData['data']['list'];
                if($priceList){
                    foreach($priceList as $k => $v){
                        $change = sprintf("%f",($v['price'] - $v['price24'])/$v['price24']);
                        $volume = $v['amount24'];
                        Db::name('currency')->where('contract_address', $v['contract'])->update(array('last_price'=>$v['price'],'change'=>$change,'volume'=>$volume));

                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

}